/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.permission.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.legendshop.dao.impl.GenericDaoImpl;
import com.legendshop.dao.support.CriteriaQuery;
import com.legendshop.dao.support.EntityCriterion;
import com.legendshop.dao.support.PageSupport;
import com.legendshop.oa.constants.SysParameterEnum;
import com.legendshop.oa.dao.UserRoleDao;
import com.legendshop.oa.model.User;
import com.legendshop.oa.model.UserRole;
import com.legendshop.oa.util.MD5Util;
import com.legendshop.oa.util.PropertiesUtil;
import com.legendshop.permission.dao.UserDao;
import com.legendshop.util.AppUtils;

/**
 * 
 * LegendShop 版权所有 2009-2011,并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得LegendShop商业授权之前，您不能将本软件应用于商业用途，否则LegendShop将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.legendesign.net
 * ----------------------------------------------------------------------------
 */
public class UserDaoImpl extends GenericDaoImpl<User, Long> implements UserDao {

	/** The Constant logger. */
	private static final Log logger = LogFactory.getLog(UserDaoImpl.class);

	private UserRoleDao userRoleDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.legendshop.permission.dao.UserDao#DeleteUserRole(java.util.List)
	 */
	public void DeleteUserRole(List<UserRole> userRoles) {
		logger.info("DeleteUserRole with size " + userRoles.size());
		userRoleDao.deleteUserRole(userRoles);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.legendshop.permission.dao.UserDao#findByName(java.lang.String)
	 */
	public List<User> findByName(String name) {
		return this.queryByProperties(new EntityCriterion().eq("name", name));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.dao.UserDao#findUserRoleByRoleId(java.lang.
	 * String)
	 */
	public List<UserRole> findUserRoleByRoleId(Long roleId) {
		return userRoleDao.getUserRoleByRoleId(roleId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.legendshop.permission.dao.UserDao#findUserRoleByUserId(java.lang.
	 * String)
	 */
	public List<UserRole> findUserRoleByUserId(Long userId) {
		return userRoleDao.getUserRoleByUserId(userId);
	}

	/**
	 * 更改用户,密码需要Md5加盐加密
	 * 
	 */
	public void updateUser(User user) {
		update(user);
	}
	
	/**
	 * 更改用户密码,密码需要Md5加盐加密
	 * 
	 */
	public void updateUserPassword(User user) {
		if (user.getPassword() != null) {
			user.setPassword(MD5Util.toMD5Password(user.getName(), user.getPassword()));
		}
		update(user);
	}

	public void setUserRoleDao(UserRoleDao userRoleDao) {
		this.userRoleDao = userRoleDao;
	}

	public User getUserByUserId(Long userId) {
		return getById(userId);
	}

	/**
	 * 保存
	 */
	public Long saveUser(User user) {
		if (user.getPassword() != null) {
			user.setPassword(MD5Util.toMD5Password(user.getName(), user.getPassword()));
		}
		return save(user);
	}

	@Override
	public PageSupport<User> queryUser(String curPageNO, String name, String enabled) {
		CriteriaQuery cq = new CriteriaQuery(User.class, curPageNO);
		cq.setPageSize(PropertiesUtil.getObject(SysParameterEnum.PAGE_SIZE, Integer.class));
		if (AppUtils.isNotBlank(name)) {
			cq.like("name", name + "%");// 1
		}
		if (AppUtils.isNotBlank(enabled)) {
			cq.eq("enabled", enabled);
		}
		return queryPage(cq);
	}
}
